{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<a href=\"https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv\" target=\"_blank\"><img align=\"left\" src=\"data/cover.jpg\" style=\"width: 76px; height: 100px; background: white; padding: 1px; border: 1px solid black; margin-right:10px;\"></a>\n",
    "*This notebook contains an excerpt from the book [Machine Learning for OpenCV](https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv) by Michael Beyeler.\n",
    "The code is released under the [MIT license](https://opensource.org/licenses/MIT),\n",
    "and is available on [GitHub](https://github.com/mbeyeler/opencv-machine-learning).*\n",
    "\n",
    "*Note that this excerpt contains only the raw code - the book is rich with additional explanations and illustrations.\n",
    "If you find this content useful, please consider supporting the work by\n",
    "[buying the book](https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv)!*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Dealing with Data Using OpenCV's TrainData Container in C++](02.05-Dealing-with-Data-Using-the-OpenCV-TrainData-Container-in-C++.ipynb) | [Contents](../README.md) | [Measuring-Model-Performance-with-Scoring-Functions](03.01-Measuring-Model-Performance-with-Scoring-Functions.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# First Steps in Supervised Learning\n",
    "\n",
    "This is the moment you've been waiting for, isn't it?\n",
    "\n",
    "We have covered all the bases - we have a functioning Python environment, we have\n",
    "OpenCV installed, and we know how to handle data in Python. Now it's time to build our\n",
    "first machine learning system! And what better way to start off than to focus on one of the\n",
    "most common and successful types of machine learning: **supervised learning**?\n",
    "\n",
    "From the previous chapter, we already know that supervised learning is all about learning\n",
    "regularities in some training data by using the labels that come with it so that we can\n",
    "predict the labels of some new, never-seen-before test data. In this chapter, we want to dig a\n",
    "little deeper, and learn how to turn our theoretical knowledge into something practical.\n",
    "\n",
    "Along the way, we want to address the following questions:\n",
    "- What's the difference between classification and regression, and when do I use which?\n",
    "- What is a $k$-nearest neighbor ($k$-NN) classifier, and how do I implement one in OpenCV?\n",
    "- How do I use logistic regression for classification, and why is it named so confusingly?\n",
    "- How do I build a linear regression model in OpenCV, and how does it differ from Lasso and ridge regression?\n",
    "\n",
    "## Outline\n",
    "\n",
    "- [Measuring Model Performance with Scoring Functions](03.01-Measuring-Model-Performance-with-Scoring-Functions.ipynb)\n",
    "- [Understanding the k-NN Algorithm](03.02-Understanding-the-k-NN-Algorithm.ipynb)\n",
    "- [Using Regression Models to Predict Continuous Outcomes](03.03-Using-Regression-Models-to-Predict-Continuous-Outcomes.ipynb)\n",
    "- [Applying Lasso and Ridge Regression](03.04-Applying-Lasso-and-Ridge-Regression.ipynb)\n",
    "- [Classifying Iris Species Using Logistic Regression](03.05-Classifying-Iris-Species-Using-Logistic-Regression.ipynb)\n",
    "\n",
    "Let's jump right in!\n",
    "\n",
    "> The book provides an overview of common supervised learnig methods, and features a detailed treatment of common machine learning workflows. Below is a summary of these topics. For more information, please refer to the book.\n",
    "\n",
    "\n",
    "## Supervised learning in OpenCV\n",
    "\n",
    "OpenCV provides a pretty straightforward interface for all its statistical\n",
    "learning models, which includes all supervised learning models.\n",
    "In OpenCV, every machine learning model derives from the `cv::ml::StatModel` base\n",
    "class. This is fancy talk for saying that if we want to be a machine learning model in\n",
    "OpenCV, we have to provide all the functionality that `StatModel` tells us to. This includes\n",
    "a method to train the model (called `train`) and a method to measure the performance of\n",
    "the model (called `calcError`).\n",
    "\n",
    "Thanks to this organization of the software, setting up a machine learning model in\n",
    "OpenCV always follows the same logic:\n",
    "- **Initialization**: We call the model by name to create an empty instance of the model.\n",
    "- **Set parameters**: If the model needs some parameters, we can set them via setter methods, which can be different for every model. For example, in order for a $k$-NN algorithm to work, we need to specify its open parameter, $k$ (as we will find out later).\n",
    "- **Train the model**: Every model must provide a method called `train`, used to fit the model to some data.\n",
    "- **Predict new labels**: Every model must provide a method called `predict`, used to predict the labels of new data.\n",
    "- **Score the model**: Every model must provide a method called `calcError`, used to measure performance. This calculation might be different for every model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Dealing with Data Using OpenCV's TrainData Container in C++](02.05-Dealing-with-Data-Using-the-OpenCV-TrainData-Container-in-C++.ipynb) | [Contents](../README.md) | [Measuring-Model-Performance-with-Scoring-Functions](03.01-Measuring-Model-Performance-with-Scoring-Functions.ipynb) >"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
